package com.youkeyi.ddy.cloud.basic.pub.organization.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.pub.mapping.entity.SysMapping;
import com.youkeyi.ddy.cloud.basic.pub.mapping.param.BusinessDeleteParam;
import com.youkeyi.ddy.cloud.basic.pub.mapping.param.BusinessQueryParam;
import com.youkeyi.ddy.cloud.basic.pub.mapping.service.SysMappingService;
import com.youkeyi.ddy.cloud.basic.pub.organization.entity.SysOrganization;
import com.youkeyi.ddy.cloud.basic.pub.organization.service.SysOrganizationService;
import com.youkeyi.ddy.cloud.basic.pub.server.entity.SysServer;
import com.youkeyi.ddy.cloud.basic.pub.server.service.SysServerService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.PublicOrganizationCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.dto.PublicOrganizationListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.dto.PublicOrganizationListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.dto.PublicOrganizationTreeItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.param.*;
import com.youkeyi.ddy.cloud.common.api.basic.pub.server.dto.PublicServerListItemCloudDto;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 组织（部门）表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-02-19
 */
@Service
public class SysOrganizationController implements PublicOrganizationCloudService {

    @Autowired
    private SysOrganizationService sysOrganizationService;
    @Autowired
    private SysMappingService sysMappingService;
    @Autowired
    private SysServerService sysServerService;

    @Override
    public PublicOrganizationListCloudDto query(PublicOrganizationQueryCloudParam param) {
        PublicOrganizationListCloudDto dto = new PublicOrganizationListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getOrganizationPrimaryKeys())) {
            param.getOrganizationPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCodes())) {
            param.getCodes().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getNames())) {
            param.getNames().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getParentKeys())) {
            param.getParentKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getNotInOrganizationPrimaryKeys())) {
            param.getNotInOrganizationPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getNotInCorpKeys())) {
            param.getNotInCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getShareCorpKeys())) {
            param.getShareCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getShareNotInCorpKeys())) {
            param.getShareNotInCorpKeys().removeAll(Collections.singleton(null));
        }

        // 需要共享的组织
        List<String> needShareOrganizationPrimaryKeys = new ArrayList<>();
        if (param.getNeedShare()) {
            QueryWrapper<SysOrganization> wrapper = new QueryWrapper<>();
            wrapper.lambda()
                    .in(CollectionUtils.isNotEmpty(param.getOrganizationPrimaryKeys()), SysOrganization::getPrimaryKey, param.getOrganizationPrimaryKeys())
                    .in(CollectionUtils.isNotEmpty(param.getShareCorpKeys()), SysOrganization::getCorpKey, param.getShareCorpKeys())
                    .notIn(CollectionUtils.isNotEmpty(param.getNotInOrganizationPrimaryKeys()), SysOrganization::getPrimaryKey, param.getNotInOrganizationPrimaryKeys())
                    .notIn(CollectionUtils.isNotEmpty(param.getShareNotInCorpKeys()), SysOrganization::getCorpKey, param.getShareNotInCorpKeys())
                    .in(CollectionUtils.isNotEmpty(param.getNames()), SysOrganization::getName, param.getNames())
                    .like(StringUtils.isNotBlank(param.getNameFuzzy()), SysOrganization::getName, param.getNameFuzzy())
                    .in(CollectionUtils.isNotEmpty(param.getCodes()), SysOrganization::getCode, param.getCodes())
                    .like(StringUtils.isNotBlank(param.getCodeFuzzy()), SysOrganization::getCode, param.getCodeFuzzy())
                    .in(CollectionUtils.isNotEmpty(param.getParentKeys()), SysOrganization::getParentKey, param.getParentKeys())
                    .eq(null != param.getLevel(), SysOrganization::getLevel, param.getLevel())
                    .eq(SysOrganization::getIsShare, 1)
                    .ne(SysOrganization::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
            ;
            List<SysOrganization> organizationList = sysOrganizationService.list(wrapper);
            if (CollectionUtils.isNotEmpty(organizationList)) {
                needShareOrganizationPrimaryKeys.addAll(organizationList.stream().map(SysOrganization::getPrimaryKey).collect(Collectors.toList()));
            }
        }

        QueryWrapper<SysOrganization> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getOrganizationPrimaryKeys()), SysOrganization::getPrimaryKey, param.getOrganizationPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), SysOrganization::getCorpKey, param.getCorpKeys())
                .notIn(CollectionUtils.isNotEmpty(param.getNotInOrganizationPrimaryKeys()), SysOrganization::getPrimaryKey, param.getNotInOrganizationPrimaryKeys())
                .notIn(CollectionUtils.isNotEmpty(param.getNotInCorpKeys()), SysOrganization::getCorpKey, param.getNotInCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getNames()), SysOrganization::getName, param.getNames())
                .like(StringUtils.isNotBlank(param.getNameFuzzy()), SysOrganization::getName, param.getNameFuzzy())
                .in(CollectionUtils.isNotEmpty(param.getCodes()), SysOrganization::getCode, param.getCodes())
                .like(StringUtils.isNotBlank(param.getCodeFuzzy()), SysOrganization::getCode, param.getCodeFuzzy())
                .in(CollectionUtils.isNotEmpty(param.getParentKeys()), SysOrganization::getParentKey, param.getParentKeys())
                .eq(null != param.getLevel(), SysOrganization::getLevel, param.getLevel())
                .ne(SysOrganization::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;
        if (CollectionUtils.isNotEmpty(needShareOrganizationPrimaryKeys)) {
            wrapper.lambda().or().in(SysOrganization::getPrimaryKey, needShareOrganizationPrimaryKeys);
        }

        Page<SysOrganization> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysOrganization> organizationList = sysOrganizationService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(organizationList.getRecords())) {
            dto.setTotal(organizationList.getTotal());

            // 查询映射数据
            BusinessQueryParam queryMappingParam = new BusinessQueryParam();
            queryMappingParam.setMappingType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_TYPE_ORGANIZATION_SERVER.value());
            queryMappingParam.setKeyType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_KEY_TYPE_SOURCE.value());
            queryMappingParam.setKeys(param.getOrganizationPrimaryKeys());
            Map<String, List<String>> serverKeysMap = new HashMap<>();
            Map<String, SysServer> serverMap = new HashMap<>();
            List<SysMapping> sysMappings = sysMappingService.businessQuery(queryMappingParam);
            if (CollectionUtils.isNotEmpty(sysMappings)) {
                List<String> targetKeys = new ArrayList<>();
                sysMappings.forEach(mapping -> {
                    if (!serverKeysMap.containsKey(mapping.getTargetKey())) {
                        serverKeysMap.put(mapping.getTargetKey(), new ArrayList<>());
                    }
                    serverKeysMap.get(mapping.getTargetKey()).add(mapping.getSourceKey());

                    targetKeys.add(mapping.getTargetKey());
                });

                // 去查询服务数据
                if (param.getNeedServer()) {
                    QueryWrapper<SysServer> queryServerWrapper = new QueryWrapper<>();
                    queryServerWrapper.lambda()
                            .in(CollectionUtils.isNotEmpty(targetKeys), SysServer::getPrimaryKey, targetKeys)
                            .ne(SysServer::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
                    ;
                    List<SysServer> servers = sysServerService.list(queryServerWrapper);
                    if (CollectionUtils.isNotEmpty(servers)) {
                        serverMap.putAll(servers.stream().collect(Collectors.toMap(SysServer::getPrimaryKey, Function.identity(), (k1, k2) -> k1)));
                    }
                }
            }


            List<PublicOrganizationListItemCloudDto> items = new ArrayList<>();
            organizationList.getRecords().forEach(organization -> {
                PublicOrganizationListItemCloudDto item = new PublicOrganizationListItemCloudDto();
                item.setOrganizationPrimaryKey(organization.getPrimaryKey());
                item.setName(organization.getName());
                item.setCode(organization.getCode());
                item.setLevel(organization.getLevel());
                item.setParentKey(organization.getParentKey());
                item.setPath(organization.getPath());
                item.setIsShare(organization.getIsShare());
                item.setCorpKey(organization.getCorpKey());
                item.setType(organization.getType());
                item.setSort(organization.getSort());
                item.setResponsibility(organization.getResponsibility());
                item.setLeaderEmployeeKey(organization.getLeaderEmployeeKey());
                item.setLeaderEmployeeName(organization.getLeaderEmployeeName());
                item.setLeaderEmployeePhone(organization.getLeaderEmployeePhone());
                item.setTelephone(organization.getTelephone());
                item.setAddress(organization.getAddress());
                item.setEmail(organization.getEmail());
                item.setDesc(organization.getDesc());
                item.setIsDeleted(organization.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(organization.getCreateTime() != null ? organization.getCreateTime().getTime() : null);
                item.setCreateUserKey(organization.getCreateUserKey());
                item.setCreateUserName(organization.getCreateUserName());
                item.setUpdateTime(organization.getUpdateTime() != null ? organization.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(organization.getUpdateUserKey());
                item.setUpdateUserName(organization.getUpdateUserName());
                if (serverKeysMap.containsKey(organization.getPrimaryKey())) {
                    item.setServerPrimaryKeys(serverKeysMap.get(organization.getPrimaryKey()));
                    if (param.getNeedServer()) {
                        if (CollectionUtils.isNotEmpty(item.getServerPrimaryKeys())) {
                            item.getServerPrimaryKeys().forEach(serverKey -> {
                                SysServer server = serverMap.get(serverKey);

                                PublicServerListItemCloudDto serverItem = new PublicServerListItemCloudDto();
                                serverItem.setServerPrimaryKey(server.getPrimaryKey());
                                serverItem.setCode(server.getCode());
                                serverItem.setName(server.getName());
                                serverItem.setLevel(server.getLevel());
                                serverItem.setParentKey(server.getParentKey());
                                serverItem.setPath(server.getPath());
                                serverItem.setDesc(server.getDesc());
                                serverItem.setRemark(server.getRemark());
                                serverItem.setIsDeleted(server.getIsDeleted());
                                serverItem.setCreateTime(server.getCreateTime() != null ? server.getCreateTime().getTime() : null);
                                serverItem.setCreateUserKey(server.getCreateUserKey());
                                serverItem.setCreateUserName(server.getCreateUserName());
                                serverItem.setUpdateTime(server.getUpdateTime() != null ? server.getUpdateTime().getTime() : null);
                                serverItem.setUpdateUserKey(server.getUpdateUserKey());
                                serverItem.setUpdateUserName(server.getUpdateUserName());
                                item.getServers().add(serverItem);
                            });
                        }
                    }
                }
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public List<PublicOrganizationTreeItemCloudDto> queryTree(PublicOrganizationQueryTreeCloudParam param) {
        QueryWrapper<SysOrganization> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .like(StringUtils.isNotBlank(param.getOrganizationPrimaryKey()), SysOrganization::getPath, param.getOrganizationPrimaryKey())
                .ne(SysOrganization::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;
        List<SysOrganization> servers = sysOrganizationService.list(wrapper);

        List<PublicOrganizationTreeItemCloudDto> tree = new ArrayList<>();
        servers.forEach(server -> {
            PublicOrganizationTreeItemCloudDto item = new PublicOrganizationTreeItemCloudDto();
            item.setOrganizationPrimaryKey(server.getPrimaryKey());
            item.setOrgCode(server.getCode());
            item.setName(server.getName());
            item.setLevel(server.getLevel());
            item.setParentKey(server.getParentKey());
            item.setPath(server.getPath());
            item.setCorpKey(server.getCorpKey());
            item.setType(server.getType());
            tree.add(item);
        });

        // 转树
        Map<String, List<PublicOrganizationTreeItemCloudDto>> serverByParentKeyMaps = tree.stream().collect(Collectors.groupingBy(PublicOrganizationTreeItemCloudDto::getParentKey));
        tree.forEach(item -> item.setOrgItems(serverByParentKeyMaps.get(item.getOrganizationPrimaryKey())));
        if (StringUtils.isBlank(param.getOrganizationPrimaryKey())) {
            return tree.stream().filter(v -> v.getOrganizationPrimaryKey().equals(param.getOrganizationPrimaryKey())).collect(Collectors.toList());
        }
        return tree;
    }

    @Override
    public ResponseContent create(PublicOrganizationCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getCorpKey())
                || StringUtils.isBlank(param.getName()) || StringUtils.isBlank(param.getCode())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysOrganization organization = new SysOrganization();
        organization.setPrimaryKey(SimpleUUidUtils.getUuid());
        organization.setCorpKey(param.getCorpKey());
        organization.setName(param.getName());
        organization.setCode(StringUtils.isNotBlank(param.getCode()) ? param.getCode() : String.format("ORG-%s", System.currentTimeMillis()));
        organization.setType(param.getType());
        organization.setSort(param.getSort());
        organization.setIsShare(param.getIsShare());
        organization.setResponsibility(param.getResponsibility());
        organization.setLeaderEmployeeKey(param.getLeaderEmployeeKey());
        organization.setLeaderEmployeeName(param.getLeaderEmployeeName());
        organization.setLeaderEmployeePhone(param.getLeaderEmployeePhone());
        organization.setTelephone(param.getTelephone());
        organization.setAddress(param.getAddress());
        organization.setEmail(param.getEmail());
        organization.setDesc(param.getDesc());
        organization.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        organization.setCreateUserKey(param.getCreateUserKey());
        organization.setCreateUserName(param.getCreateUserName());
        organization.setCreateTime(new Date());

        // 构造父级数据
        organization.setLevel(0);
        organization.setParentKey("");
        organization.setPath(String.format("|%s|", organization.getPrimaryKey()));
        if (StringUtils.isNotBlank(param.getParentKey())) {
            QueryWrapper<SysOrganization> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SysOrganization::getParentKey, param.getParentKey());
            SysOrganization parentOrganization = sysOrganizationService.getOne(queryWrapper);
            if (null != parentOrganization && StringUtils.isNotBlank(parentOrganization.getPrimaryKey())) {
                organization.setLevel(parentOrganization.getLevel() + 1);
                organization.setParentKey(parentOrganization.getPrimaryKey());
                organization.setPath(String.format("%s|", parentOrganization.getPath()));
            }
        }

        boolean save = sysOrganizationService.save(organization);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }

        // 服务数据录入
        if (CollectionUtils.isNotEmpty(param.getServerPrimaryKeys())) {
            List<SysMapping> serverCreateParams = new ArrayList<>();
            param.getServerPrimaryKeys().forEach(serverPrimaryKey -> {
                SysMapping serverMapping = new SysMapping();
                serverMapping.setPrimaryKey(SimpleUUidUtils.getUuid());
                serverMapping.setMappingType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_TYPE_ORGANIZATION_SERVER.value());
                serverMapping.setSourceKey(organization.getPrimaryKey());
                serverMapping.setTargetKey(serverPrimaryKey);
                serverMapping.setCreateUserKey(param.getCreateUserKey());
                serverMapping.setCreateUserName(param.getCreateUserName());
                serverCreateParams.add(serverMapping);
            });
            sysMappingService.businessCreate(serverCreateParams);
        }

        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(organization.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(PublicOrganizationUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getOrganizationPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }


        QueryWrapper<SysOrganization> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysOrganization::getPrimaryKey, param.getOrganizationPrimaryKey());
        SysOrganization myselfOrganization = sysOrganizationService.getOne(queryWrapper);
        if (null == myselfOrganization || StringUtils.isBlank(myselfOrganization.getPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.UNKNOWN_ERROR.getMessage());
            return rc;
        }


        UpdateWrapper<SysOrganization> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysOrganization::getPrimaryKey, param.getOrganizationPrimaryKey());
        SysOrganization organization = new SysOrganization();
        organization.setCorpKey(param.getCorpKey());
        organization.setPrimaryKey(param.getOrganizationPrimaryKey());
        organization.setName(param.getName());
        organization.setCode(param.getCode());
        organization.setType(param.getType());
        organization.setSort(param.getSort());
        organization.setIsShare(param.getIsShare());
        organization.setResponsibility(param.getResponsibility());
        organization.setLeaderEmployeeKey(param.getLeaderEmployeeKey());
        organization.setLeaderEmployeeName(param.getLeaderEmployeeName());
        organization.setLeaderEmployeePhone(param.getLeaderEmployeePhone());
        organization.setTelephone(param.getTelephone());
        organization.setAddress(param.getAddress());
        organization.setEmail(param.getEmail());
        organization.setDesc(param.getDesc());
        organization.setUpdateUserKey(param.getUpdateUserKey());
        organization.setUpdateUserName(param.getUpdateUserName());
        organization.setUpdateTime(new Date());

        if (!myselfOrganization.getParentKey().equals(param.getParentKey())) {
            // 构造父级数据
            organization.setLevel(0);
            organization.setParentKey("");
            organization.setPath(String.format("|%s|", organization.getPrimaryKey()));
            if (StringUtils.isNotBlank(param.getParentKey())) {
                QueryWrapper<SysOrganization> queryParentWrapper = new QueryWrapper<>();
                queryParentWrapper.lambda().eq(SysOrganization::getPrimaryKey, param.getParentKey());
                SysOrganization parentOrganization = sysOrganizationService.getOne(queryParentWrapper);
                if (null != parentOrganization && StringUtils.isNotBlank(parentOrganization.getPrimaryKey())) {
                    organization.setLevel(parentOrganization.getLevel() + 1);
                    organization.setParentKey(parentOrganization.getPrimaryKey());
                    organization.setPath(String.format("%s|", parentOrganization.getPath()));
                }
            }
        }

        boolean save = sysOrganizationService.update(organization, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }

        // 删除映射数据
        BusinessDeleteParam deleteMappingParam = new BusinessDeleteParam();
        deleteMappingParam.setMappingType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_TYPE_ORGANIZATION_SERVER.value());
        deleteMappingParam.setKeyType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_KEY_TYPE_SOURCE.value());
        deleteMappingParam.setKeys(Collections.singletonList(param.getOrganizationPrimaryKey()));
        sysMappingService.businessDelete(deleteMappingParam);
        // 重新加入映射数据
        if (CollectionUtils.isNotEmpty(param.getServerPrimaryKeys())) {
            List<SysMapping> serverCreateParams = new ArrayList<>();
            param.getServerPrimaryKeys().forEach(serverPrimaryKey -> {
                SysMapping serverMapping = new SysMapping();
                serverMapping.setPrimaryKey(SimpleUUidUtils.getUuid());
                serverMapping.setMappingType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_TYPE_ORGANIZATION_SERVER.value());
                serverMapping.setSourceKey(organization.getPrimaryKey());
                serverMapping.setTargetKey(serverPrimaryKey);
                serverMapping.setCreateUserKey(param.getUpdateUserKey());
                serverMapping.setCreateUserName(param.getUpdateUserName());
                serverCreateParams.add(serverMapping);
            });
            sysMappingService.businessCreate(serverCreateParams);
        }

        return rc;
    }

    @Override
    public ResponseContent delete(PublicOrganizationDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getOrganizationPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysOrganization> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysOrganization::getPrimaryKey, param.getOrganizationPrimaryKeys());

        SysOrganization organization = new SysOrganization();
        organization.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        organization.setUpdateUserKey(param.getUpdateUserKey());
        organization.setUpdateUserName(param.getUpdateUserName());
        organization.setUpdateTime(new Date());
        boolean save = sysOrganizationService.update(organization, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }

        // 删除映射数据
        BusinessDeleteParam deleteMappingParam = new BusinessDeleteParam();
        deleteMappingParam.setMappingType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_TYPE_ORGANIZATION_SERVER.value());
        deleteMappingParam.setKeyType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_KEY_TYPE_SOURCE.value());
        deleteMappingParam.setKeys(param.getOrganizationPrimaryKeys());
        sysMappingService.businessDelete(deleteMappingParam);
        return rc;
    }
}
